<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="地区编码" prop="cityCode">
        <el-input
          v-model="queryParams.cityCode"
          placeholder="请输入地区编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省市区" prop="provincial">
        <el-input
          v-model="queryParams.provincial"
          placeholder="请输入省市区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          v-hasPermi="['addressInfo:addressInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExport"
          v-hasPermi="['addressInfo:addressInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="addressInfoList" row-key="cityCode" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" >
      <el-table-column label="城市" align="left" prop="city" />
      <el-table-column label="地区编码" align="center" prop="cityCode" />
      <el-table-column label="类型" align="center" prop="type" >
        <template slot-scope="scope">
          {{ scope.row.type == 1 ? '省': '市' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['addressInfo:addressInfo:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改地址列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="${comment}" prop="provinceCode">
          <el-input v-model="form.provinceCode" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="city">
          <el-input v-model="form.city" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="province">
          <el-input v-model="form.province" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="aliasName">
          <el-input v-model="form.aliasName" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="sortWeight">
          <el-input v-model="form.sortWeight" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="${comment}" prop="parentId">
          <el-input v-model="form.parentId" placeholder="请输入${comment}" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAddressInfo, getAddressInfo, delAddressInfo, addAddressInfo, updateAddressInfo } from "@/api/addressInfo/addressInfo";

export default {
  name: "AddressInfo",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 地址列表表格数据
      addressInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        provincial: null,
        cityCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        city: [
          { required: true, message: "$comment不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询地址列表列表 */
    getList() {
      this.loading = true;
      listAddressInfo(this.queryParams).then(response => {
        this.addressInfoList = this.handleTree(response.rows, "cityCode");
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        provincial: null,
        cityCode: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cityCode)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      // this.reset();
      // this.open = true;
      // this.title = "添加地址列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // this.reset();
      // const cityCode = row.cityCode || this.ids
      // getAddressInfo(cityCode).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改地址列表";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cityCode != null) {
            updateAddressInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAddressInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cityCodes = row.cityCode || this.ids;
      this.$modal.confirm('是否确认删除地址列表编号为"' + cityCodes + '"的数据项？').then(function() {
        return delAddressInfo(cityCodes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('addressInfo/addressInfo/export', {
        ...this.queryParams
      }, `addressInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
